const {login, register} = require("../controller/userController");
const {SuccessModel,ErrorModel} = require("../utils/resModel");
const jwt = require("koa-jwt");
const router = require('koa-router')()
const {getToken} = require('../utils/cryp')

router.prefix('/user')

router.post('/login', async (ctx, next) => {
  console.log('进来了')
  const body = ctx.request.body
  const data = await login(ctx,body)
  if (data && data.username) {
    const token = getToken(data);
    ctx.body = new SuccessModel({
      token: token,
      userName: data.username
    },'OK!登录成功')
  }else{
    ctx.body = new ErrorModel('用户名或密码错误')
  }
})

router.post('/register', async (ctx, next) =>{
  const body = ctx.request.body
  const data = await register(ctx,body)
  if (data && data['dataValues']){
    ctx.body = new SuccessModel('OK!注册成功,自动登陆')
  }
})

module.exports = router

